package com.dulichdanang.searchLocation;

import java.util.ArrayList;
import java.util.List;

import com.dulichdanang.R;
import com.dulichdanang.R.layout;
import com.dulichdanang.R.menu;
import com.dulichdanang.location.locations.LocationsActivity;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class SearchLocationActivity extends Activity implements LocationListener {

	//private Double latitude,longitude;
	// This is demo location int emulator, when install on phone, use upper
	private Double longitude=108.19505914999999,latitude=16.06474635;
	LocationManager locationManager ;
	String provider;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_location);
		final Spinner s =(Spinner)findViewById(R.id.spLocationCatSearchLocation);
		final EditText txtName = (EditText)findViewById(R.id.txtlocationNameSearchLocation);
		final CheckBox cbNear = (CheckBox)findViewById(R.id.cbNearSearchLocation);
		final Button btnSearch = (Button)findViewById(R.id.btnSearchLocation);
		//List locationCat
		List<String> list = new ArrayList<String>();
		list.add("Haha");list.add("Haha");list.add("Haha");list.add("Haha");list.add("Haha");
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter);
		//Get current location
			locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);        
	        Criteria criteria = new Criteria();
	        provider = locationManager.getBestProvider(criteria, false);         
	        if(provider!=null && !provider.equals("")){
	            Location location = locationManager.getLastKnownLocation(provider);          
	            locationManager.requestLocationUpdates(provider, 20000, 1, this);
	            if(location!=null)
	            	onLocationChanged(location);//
	            else
	            	Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();
	        }else{
	        	Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
	        }
		OnClickListener btnClick = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String id = "1", near = "no";
				String name = txtName.getText().toString();
				if(cbNear.isChecked()) {
					near = "yes";
				}
				Intent i = new Intent(SearchLocationActivity.this,LocationsActivity.class);
				//data is : search_location(action)-latitude-longitude-id-name-near
				i.putExtra("data","search_location"+"-"+latitude+"-"+longitude+"-"+id+"-"+name+""+near);
				startActivity(i);
			}
		};
		btnSearch.setOnClickListener(btnClick);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_search_location, menu);
		return true;
	}

	@Override
	public void onLocationChanged(Location arg0) {
		// TODO Auto-generated method stub
		this.latitude = arg0.getLatitude();
		this.longitude = arg0.getLongitude();
	}

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}

}
